#!/bin/bash
mysql_ready() {
    mysqladmin ping --host=$MYSQL_HOST --user=$MYSQL_USER --password=$MYSQL_PASSWORD >/dev/null 2>&1
}

while ! (mysql_ready); do
    sleep 3
    echo "Waiting for database connection ..."
done

if ! mysql -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE -e "DESCRIBE ${TABLE_PREFIX}users;" >/dev/null 2>&1; then
    # user 表不存在，执行导入操作
    echo "Importing SQL data..."
    mysql -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE < /var/www/html/mysql.sql
else
    # user 表已存在，跳过导入操作
    echo "User table already exists, skipping SQL import..."
fi

# 执行其他的初始化操作
echo "Initializing..."
chown -Rf www-data:www-data /var/www/html/runtime
chown -Rf www-data:www-data /var/www/html/public

# 启动服务
echo "Starting Apache server..."

exec "$@"